﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Core.Controllers
{
	using System.Web.Mvc;
	using Falcon.Core.Domain;
	using Falcon.Core.Domain.Services;
	using Falcon.Core.Domain.Values;
	using Falcon.Core.Extensions;
	using Falcon.Core.Validation;

	[Authorize]
	public class SystemTimeZoneController : DefaultController
	{
		public SystemTimeZoneController(ITimeZoneService timeZoneService)
		{
			this.TimeZoneService = timeZoneService;
		}

		public ITimeZoneService TimeZoneService
		{
			get;
			private set;
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult Index(int? page)
		{
			var timeZones = TimeZoneService.GetPaged(new TimeZoneSpecification
			{
				Page = page,
				Limit = Settings.TimeZones.PageLimit
			});

			var timeZone = timeZones.FirstOrDefault();

			return timeZone.CanView ? View(Views.Index, Masters.System, timeZones) : NotAuthorized();
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult Create()
		{
			var timeZone = TimeZoneService.Create();

			return timeZone.CanCreate ? View(Views.Create, Masters.System, new TimeZoneCreate()) : NotAuthorized();
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Create(TimeZoneCreate value)
		{
			try
			{
				var timeZone = TimeZoneService.Create();

				if (!timeZone.CanCreate)
				{
					return NotAuthorized();
				}

				this.TimeZoneService.Insert(timeZone, value);

				return Message(Routes.SystemTimeZoneUpdate, new { id = timeZone.Id }, Resources.Messages.TimeZoneCreated.Fmt(timeZone.Title));
			}
			catch (ValidationException exception)
			{
				ModelState.CopyValidationExceptions(exception);
			}

			return View(Views.Create, Masters.System, value);
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult Update(int id)
		{
			var timeZone = TimeZoneService.GetById(id);

			if (timeZone == null)
			{
				return Error(Resources.Messages.TimeZoneNotFound);
			}

			return timeZone.CanUpdate ? View(Views.Update, Masters.System, new TimeZoneUpdate(timeZone)) : NotAuthorized();
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Update(TimeZoneUpdate value)
		{
			try
			{
				var timeZone = TimeZoneService.GetById(value.Id);

				if (timeZone == null)
				{
					return Error(Resources.Messages.TimeZoneNotFound);
				}

				if (!timeZone.CanUpdate)
				{
					return NotAuthorized();
				}

				this.TimeZoneService.Update(timeZone, value);

				return Message(Routes.SystemTimeZoneUpdate, new { id = timeZone.Id }, Resources.Messages.TimeZoneUpdated.Fmt(timeZone.Title));
			}
			catch (ValidationException exception)
			{
				ModelState.CopyValidationExceptions(exception);
			}

			return View(Views.Update, Masters.System, value);
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult Delete(int id)
		{
			var timeZone = this.TimeZoneService.GetById(id);

			if (timeZone == null)
			{
				return base.Error(Resources.Messages.TimeZoneNotFound);
			}

			return timeZone.CanDelete ? base.View(Views.Delete, Masters.System, new TimeZoneDelete(timeZone)) : NotAuthorized();
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Delete(TimeZoneDelete value)
		{
			try
			{
				var timeZone = this.TimeZoneService.GetById(value.Id);

				if (timeZone == null)
				{
					return base.Error(Resources.Messages.TimeZoneNotFound);
				}

				if (!timeZone.CanDelete)
				{
					return NotAuthorized();
				}

				this.TimeZoneService.Delete(timeZone);

				return base.RedirectToRoute(Routes.SystemTimeZoneIndex);
			}
			catch (ValidationException exception)
			{
				base.ModelState.CopyValidationExceptions(exception);
			}

			return base.View(Views.Delete, Masters.System, value);
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult Setup()
		{
			var timeZone = this.TimeZoneService.Create();

			if (!timeZone.CanCreate)
			{
				return NotAuthorized();
			}

			var count = this.TimeZoneService.Count();

			if (count > 0)
			{
				return base.Error(Resources.Messages.TimeZoneAlreadySetup);
			}

			this.TimeZoneService.Setup();

			return base.RedirectToRoute(Routes.SystemTimeZoneIndex);
		}
	}
}